//
//  VVSportTrack.m
//  DingDingSport
//
//  Created by 王惠平 on 2017/4/22.
//  Copyright © 2017年 will wang. All rights reserved.
//

#import "VVSportTrack.h"
#import "VVSportSpeaker.h"

@implementation VVSportTrack {
    
    //记录的每一段折线起点
    CLLocation * _source;

    //折线集合
    NSMutableArray<VVSportPolyline*> *_polyline;
    
    //记录上一次的GPS位置信息
    CLLocation *_preGPSLoaction;
    
    //语音播报器
    VVSportSpeaker *_speaker;

}

@synthesize totalTime = _totalTime;
@synthesize totalDistance = _totalDistance;
@synthesize avgSpeed = _avgSpeed;
@synthesize maxSpeed = _maxSpeed;

- (instancetype)initWithSportType:(VVSportType)sportType withSportState:(VVSportState)state
{
    self = [super init];
    if (self) {
        
        //记录运动类型
        _sportType = sportType;
        //设置运动类型图片
        //判断运动类型
        NSString *sportTypeImgName;
        switch (_sportType) {
            case VVSportTypeWalking:
                sportTypeImgName = @"map_annotation_walk";
                break;
            case VVSportTypeRunning:
                sportTypeImgName = @"map_annotation_run";
                break;
            case VVSportTypeRiding:
                sportTypeImgName = @"map_annotation_bike";
                break;
            default:
                break;
        }
        _sportTypeImageName = sportTypeImgName;
        
        //记录运动状态
        _sportState = state;
        
        _polyline = [NSMutableArray array];
        
        //创建播报器
        _speaker = [[VVSportSpeaker alloc]init];
        
        //播报开始运动
        [_speaker startSportWithSportType:_sportType];
        
    }
    return self;
}


- (VVSportPolyline *)appendPolylineWithDestCoord:(CLLocation*)dest {
    
    //整距离播报
    [_speaker reportSportWithDistance:_totalDistance andTime:_totalTime andAvgSpeed:_avgSpeed];
    
    //计算GPS强度
    if ([self calculateGPSStateWithLocation:dest] < VVSportGPSStateNormal) {
        
        return nil;
    }
    
    //判断运动状态
    if (self.sportState != VVSportStateContinue) {
        
        //暂停\结束运动时 ,停止生产折线
        //清空记录的折线起点
        _source = nil;
        
        return nil;
    }
    
    
    //判断首次调用
    if (_source == nil) {
        
        //记录起点
        _source = dest;
        
        //生成起点大头针模型
        _startAnno = [[MAPointAnnotation alloc]init];
        _startAnno.coordinate = dest.coordinate;
        
        return nil;
    }
    
   //创建折线
    VVSportPolyline *polyline = [VVSportPolyline polylineWithSourceLocation:_source andDestLocation:dest];

    //记录下一段的起点
    _source = dest;
    
    //将折线模型进行记录
    [_polyline addObject:polyline];
    
    return polyline;
    
}

//计算GPS强度
- (VVSportGPSState)calculateGPSStateWithLocation:(CLLocation *)location {
    
    //默认GPS关闭
    VVSportGPSState state = VVSportGPSStateBad;
    
    //首先判断是否在室内
    if (location.speed < 0) {
        
        //发送GPS跟新的通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"VVSportGPSStateDidChangeNote" object:nil userInfo:@{@"VVSportGPSStateDidChangeNoteGPSStateKey":@(state)}];
        
        return state;
    }
    
    if (_preGPSLoaction == nil) {
        
        _preGPSLoaction = location;
        
        return state;
    }
    
    //GPS精度
    double deltaTime = ABS([location.timestamp timeIntervalSinceDate:_preGPSLoaction.timestamp]-1);
    
    if (deltaTime > 1) {
        
        //差
        
    } else if (deltaTime <= 1 && deltaTime >= 0.01) {
        
        //一般
        state = VVSportGPSStateNormal;
    } else {
        
        //好
        state = VVSportGPSStateGood;
    }
    
    _preGPSLoaction = location;
    
    //发送GPS跟新的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"VVSportGPSStateDidChangeNote" object:nil userInfo:@{@"VVSportGPSStateDidChangeNoteGPSStateKey":@(state)}];
    
    return state;
}

#pragma  mark - setter & getter
- (void)setSportState:(VVSportState)sportState{
    
    _sportState = sportState;
    
    //播报运动状态
    [_speaker changeSportState:_sportState];
}

- (CGFloat)totalTime {
    
    //求集合中元素的某个属性的和 可以使用kvc来进行运算
    _totalTime = [[_polyline valueForKeyPath:@"@sum.time"] floatValue];
    
    return _totalTime;
}

- (CGFloat)totalDistance {
    
    _totalDistance = [[_polyline valueForKeyPath:@"@sum.distance"] floatValue];
    
    return _totalDistance;
}

- (CGFloat)avgSpeed {
    
    _avgSpeed = [[_polyline valueForKeyPath:@"@avg.speed"] floatValue];
    
    return _avgSpeed;
}

- (CGFloat)maxSpeed {
    
    _maxSpeed = [[_polyline valueForKeyPath:@"@max.speed"] floatValue];
    
    return _maxSpeed;
}

- (NSString *)timeStr{
    
    int time = (int) _totalTime;
    return [NSString stringWithFormat:@"%02d:%02d:%02d", time / 3600, (time % 3600) / 60, time % 60];
}

@end
